$(function () {
    $("#jqGrid").Grid({
        url: '../applyopenshop/list',
        colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '申请人姓名', name: 'name', index: 'name', width: 80},
			{label: '申请人手机号', name: 'mobile', index: 'mobile', width: 80},
            {label: '状态', name: 'status', index: 'status', width: 80 ,formatter: function (value, options, row) {
                if(value == 0){
                  return  '<span class="label label-danger">处理中</span>'
                }if(value ==1){
                	return '<span class="label label-success">已处理</span>';
				}if(value ==2 ){
                    return '<span class="label label-fail">已拒绝</span>';
				}
                }},
            {label: '寝室楼', name: 'buildingName', index: 'building_name', width: 80},
            {label: '学校名称', name: 'schoolName', index: 'school_name', width: 80},
			{label: '申请人补充信息', name: 'remarks', index: 'remarks', width: 80},
			{label: '备注信息', name: 'adminRemarks', index: 'admin_remarks', width: 80},
            {label: '用户id', name: 'userId', index: 'user_id', hidden: true},
            {label: '学校id', name: 'schoolId', index: 'school_id', hidden: true},
            {label: '寝室楼id', name: 'buildingId', index: 'building_id',hidden: true}
    ]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		applyOpenshop: {
            schoolId:0
        },
        schoolList: [],
        allDormList:[],
        dormList:[],
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
        uploadList:[],
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.applyOpenshop = {};
		},
		update: function (event) {
            let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            let url = vm.applyOpenshop.id == null ? "../applyopenshop/save" : "../applyopenshop/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.applyOpenshop),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../applyopenshop/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(id){
            Ajax.request({
                url: "../applyopenshop/info/"+id,
                async: true,
                successCallback: function (r) {
                    vm.applyOpenshop = r.applyopenshop;
                }
            });
		},
        handleView(name) {
            this.imgName = name;
            this.visible = true;
        },
        handleRemove(file) {
            // 从 upload 实例删除数据
            const fileList = this.uploadList;
            this.uploadList.splice(fileList.indexOf(file), 1);
        },
        handleSuccess(res, file) {
            // 因为上传过程为实例，这里模拟添加 url
            file.imgUrl = res.url;
            file.name = res.url;
            vm.uploadList.add(file);
        },
        handleBeforeUpload() {
            const check = this.uploadList.length < 5;
            if (!check) {
                this.$Notice.warning({
                    title: '最多只能上传 5 张图片。'
                });
            }
            return check;
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleSuccessPositiveUrl: function (res, file) {
            vm.applyOpenshop.idCardPositiveUrl = file.response.url;
        },
        handleAnywayPicUrl: function (res, file) {
            vm.applyOpenshop.idCardAnywayUrl = file.response.url;
        },
        handleSuccessStudentCardUrl: function (res, file) {
            vm.applyOpenshop.studentCardUrl = file.response.url;
        },
        eyePositiveUrl: function () {
            var url = vm.applyOpenshop.idCardPositiveUrl;
            eyeImage(url);
        },
        eyeAnywayUrl: function () {
            var url = vm.applyOpenshop.idCardAnywayUrl;
            eyeImage(url);
        },
        eyeStudentCardUrl: function () {
            var url = vm.applyOpenshop.studentCardUrl;
            eyeImage(url);
        },
        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        },

		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	},
    created:function(){
        var that = this;
        //获取所有的学校和宿舍
        Ajax.request({
            type: "POST",
            url: "../regionrelatebedroom/queryAllSchool",
            contentType: "application/json",
            successCallback: function (date) {
                that.schoolList=date.list;
            }
        });
        Ajax.request({
            type: "POST",
            url: "../regionrelatebedroom/queryAllDorm",
            contentType: "application/json",
            successCallback: function (date) {
                that.allDormList=date.list;
            }
        });
    },

    watch: {
        'applyOpenshop.schoolId': {
            handler() {
                this.dormList = [];
                debugger;
                this.allDormList.forEach((item, index) => {
                    if (item.parentId === this.applyOpenshop.schoolId) {
                        this.dormList.push(item);
                    }
                })
            },
            deep: true
        }
    }
});